<?php
/*
 * @ Nguyễn Như Khang
 * nhukhang095@gmail.com
 */
class Pager{
	function drawPager($totalItems, $pageSize, $currentPage){
		$pages = (int)($totalItems / $pageSize);
		$tmp1 = $totalItems % $pageSize;
		if($tmp1 > 0) $pages += 1;
		
		if($pages > 1){
			$str = "<div class=\"pagination\"><ul>";
			for($i = 0; $i < $pages; $i++){
				if($i + 1 != $currentPage){
					$sort = isset($_GET['s']) ? $_GET['s'] : null;
					$dir = isset($_GET['d']) ? $_GET['d'] : null;
					
					$str .= '<li><a href="' . $_SERVER['PHP_SELF'] . '?p=' . ($i + 1);
					if(isset($sort)){
						$str .= "&s=" . $sort;
					} 
					if(isset($dir)){
						$str .= "&d=" . $dir;
					}
					$str .= '">' . ($i + 1) . "</a></li> ";
				}else{
					$str .= "<li class='disabled'><span class='current'>" . ($i + 1) . "</span></li>";
				}
			}	
			echo $str. '</ul></div>';
		}else if($pages > 0){
			echo '<span class="text-info">Hiển thị tất cả phần tử</span>';
		}
	}
	
	function drawResults($totalItems){
		if($totalItems == 0){
			echo '<span class="text-warning">Không tìm thấy phần tử nào</span>';
		}else if($totalItems > 0){
			echo '<span class="text-info">Tìm thấy ' . $totalItems . ' phần tử</span>';
		}
	}
	
	function drawSortBy($field){
		$url = $_SERVER['PHP_SELF'];
		
		$page = isset($_GET['p']) ? $_GET['p'] : null;
		$dir = isset($_GET['d']) ? $_GET['d'] : null;
		
		if(isset($page)){
			$url .= "?p=" . $page . "&s=" . $field;
		}else{
			$url .= "?s=" . $field;
		}
		if(isset($dir)){
			if($dir == "asc"){
				$url .= "&d=desc";	
			}else{
				$url .= "&d=asc";
			}
		}else{
			$url .= "&d=ASC";
		}
		echo $url;
	}
	
}
?>